package com.china08.yxyapi.api.manage.tec.teaching.scoreport.lequestionanalysis;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResResourceBookContentRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;

/**
 * 成绩分析     小题分析
 * 
 */
@CrossOrigin
@RestController
public class LeQuestionAnalysisCharpterController {
	@Autowired
	private YxyResBookRepository  yxyResBookRepository;
	@Autowired
	private ExamRepository  examRepository;
	@Autowired
	private YxyResResourceBookContentRepository  yxyResResourceBookContentRepository;
	@Autowired
    private YxyResBookContentsRepository yrbcr;
	@RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/lequestionanalysis/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response,@RequestBody ReqModel reqModel){
		RespModel model=new RespModel();
		Exam exam = examRepository.findById(reqModel.examId);
    	//通过学段名称,版本id,学科名称获取到策别id,策别name
		List<Object[]> semersterLst = yxyResBookRepository.getVerson(exam.getSubjectName(),exam.getStage(),exam.getVersion());
		
		SemersterModel semersterModel=null;
		//对策别进行封装
		List<SemersterModel> semersterModels = new ArrayList<SemersterModel>();
		for (Object[] semerster : semersterLst) {
			semersterModel = new SemersterModel();
			semersterModel.semersterId = semerster[0].toString();//策别id
			semersterModel.semersterName = semerster[1].toString();//策别name
			semersterModels.add(semersterModel);
		}
		
		if (reqModel.semersterId !=null &&!reqModel.semersterId.equals("")) {
			BookContentRespModel bookContentRespModel=null;
			List<BookContentRespModel> bookContentRespModelLst = new ArrayList<BookContentRespModel>();
			List<Object> charpterList=yxyResResourceBookContentRepository.getCharpter(exam.getStage(), reqModel.semersterId,exam.getSubjectName(), exam.getVersion());
			 for (Object objects : charpterList) {
			    	List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(objects.toString(), 1);
			        for (YxyResBookContents yxyResBookContents : listcoContents) {
			            bookContentRespModel = new BookContentRespModel();
				    	bookContentRespModel.id = yxyResBookContents.getId();
				    	bookContentRespModel.name = yxyResBookContents.getName();
				    	bookContentRespModel.pId = yxyResBookContents.getpId();
				    	bookContentRespModelLst.add(bookContentRespModel);
			        }
				}
		     Collections.sort(bookContentRespModelLst, new Comparator<BookContentRespModel>(){  
				       @Override  
				       public int compare(BookContentRespModel entry1, BookContentRespModel entry2) {  
				 	//     int value1 = Integer.valueOf(entry1.getValue());  
				 	//     int value2 = Integer.valueOf(entry2.getValue());  
				 	//     return value1 - value2;  
				 	       return entry1.id.compareTo(entry2.id);  
				 	   }                 
			 }); 

			 model.bookContent = bookContentRespModelLst;
		}
		model.semersterModels = semersterModels;
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
    	//策别集合
    	public List<SemersterModel> semersterModels;
    	//章节id、章节父级id、章节name
    	public List<BookContentRespModel> bookContent;
        public List<BookContentRespModel> getBookContent() {
            return bookContent;
        }
        public void setBookContent(List<BookContentRespModel> bookContent) {
            this.bookContent = bookContent;
        }
    }
    protected static class ReqModel extends YxyApiReqModel {
    	//考试id
    	public String examId;
    	//策别id
    	public String semersterId;
    }
    public class SemersterModel{
    	//策别id
    	public String semersterId;
    	//策别name
    	public String semersterName;
    }
    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }
}
